Introducion to history browser

Lukasz A. Bartnik

2017-12-16

library(dplyr)
library(lubridate)
library(magrittr)
library(ggplot2)
input <-
  system.file("extdata/block_62.csv", package = "experiment") %>%
  readr::read_csv(na = 'Null') %>%
  rename(meter = LCLid, timestamp = tstp, usage = `energy(kWh/hh)`) %>%
  filter(meter %in% c("MAC004929", "MAC000010", "MAC004391"),
         year(timestamp) == 2013)
#> Parsed with column specification:
#> cols(
#>   LCLid = col_character(),
#>   tstp = col_datetime(format = ""),
#>   `energy(kWh/hh)` = col_double()
#> )
input %<>%
  mutate(timestamp = floor_date(timestamp, 'hours')) %>%
  group_by(meter, timestamp) %>%
  summarise(usage = sum(usage))
input %<>% filter(meter == "MAC004929")
with(input, plot(timestamp, usage, type = 'p', pch = '.'))

x <-
  input %>%
  mutate(hour = hour(timestamp),
         dow  = wday(timestamp)) %>%
  mutate_at(vars(hour, dow), funs(as.factor))
m <- lm(usage ~ hour:dow, x)
x <-
  input %>%
  mutate(hour = hour(timestamp),
         dow  = wday(timestamp, label = TRUE)) %>%
  mutate_at(vars(hour, dow), funs(as.factor)) %>%
  group_by(hour, dow) %>%
  summarise(usage = mean(usage, na.rm = TRUE))
with(x, plot(hour, usage))

ggplot(x) + geom_point(aes(x = hour, y = usage)) + facet_wrap(~dow)

ggplot(x) + geom_boxplot(aes(x = hour, y = usage)) + facet_wrap(~dow)

plot(fullhistory())

Let’s go back to the last step before narrowing down to just one meter. Clicking on the second input node in the history window copies this object’s identifier, which we can then use to bring back the state of R session just after it was created.

restore('40692a8b662cc0327e12076193c13a8b730d2fc0')

Now we can try a different house.

input %<>% filter(meter == "MAC000010")
x <-
  input %>%
  mutate(hour = hour(timestamp),
         dow  = wday(timestamp, label = TRUE)) %>%
  mutate_at(vars(hour, dow), funs(as.factor)) %>%
  group_by(hour, dow) %>%
  summarise(usage = mean(usage, na.rm = TRUE))
ggplot(x) + geom_boxplot(aes(x = hour, y = usage)) + facet_wrap(~dow)

The history looks different now, there is a second branch reflecting the last three commands we issued.

plot(fullhistory())

OK, so how about the third house in the data set? We restore the same point in time again, and repeat the commands.

restore('40692a8b662cc0327e12076193c13a8b730d2fc0')
input %<>% filter(meter == "MAC004391")
x <-
  input %>%
  mutate(hour = hour(timestamp),
         dow  = wday(timestamp, label = TRUE)) %>%
  mutate_at(vars(hour, dow), funs(as.factor)) %>%
  group_by(hour, dow) %>%
  summarise(usage = mean(usage, na.rm = TRUE))
ggplot(x) + geom_boxplot(aes(x = hour, y = usage)) + facet_wrap(~dow)

plot(fullhistory())